package com.mos.service.impl;

import com.mos.dao.MenuDao;
import com.mos.entity.Menu;
import com.mos.service.AccessoryService;
import com.mos.service.MenuService;
import com.mos.support.AccSupport;
import com.mos.support.enumeration.AccessoryType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Package Name: com.mos.service.impl
 * User: grq
 * Date: 2016/3/29
 * Time: 11:02
 * Description:some des here!
 */
@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Autowired
    private AccessoryService accessoryService;

    @Transactional(readOnly = false)
    public int save(Menu menu, String systemIconName, String systemIconPath, String type) throws Exception {
        Map<String, AccSupport> accMap = menu.getAccMap();
        String iconId = "";
        if (AccessoryType.SYSTEM.getCode().equals(type)) {
            iconId = accessoryService.saveUploadFile(systemIconPath, systemIconName, type);
        } else {
            accMap.get("icon");
        }
        menu.setIcon(iconId);
        return menuDao.save(menu);
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> queryList(Object[] params) {
//        List<Map<String, Object>> menus = menuDao.queryList(params);

        return menuDao.queryList(params);
    }

    @Transactional(readOnly = true)
    public Menu findById(String id) {
        return menuDao.findById(id);
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> queryListByPid(String pid) {
        List<Map<String, Object>> menus = new ArrayList<Map<String, Object>>();
        if ("0".equals(pid)) {
            Map<String, Object> menu = menuDao.getRoot();
            menus.add(menu);
        }else {
            menus = menuDao.queryListByPid(pid);
        }
        return menus;
    }
}
